/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#ifndef MK_HTTP_CLIENT_POOL_H
#define MK_HTTP_CLIENT_POOL_H
#include "MkHttp/MkHttpClient.h"
#include "MkUtil/MkLock.h"
#include <json/json.h>

typedef MkFunction<void(Uint32 ErrorCode, const Json::Value &ResBody)>   MkHttpResponseJsonCb;
class CMkHttpClient;
class CMkTimer;
class CMkEventPool;
class MkApi CMkHttpClientPool
{
public:
    CMkHttpClientPool(CMkEventPool* pEventPool, CMkTimer* pTimer);
    virtual ~CMkHttpClientPool();
    Uint32 SendJsonRequest(const MkHttpMethod& Method, const MkString& HttpUrl, const MkMapString& MapParam, const MkMapString& MapHeader, const Json::Value &Request, const MkHttpResponseJsonCb& Cb, Uint32 TimeMilSeconds = 5000);
    Uint32 SendRequest(const MkHttpMethod& Method, const MkString& HttpUrl, const MkMapString& MapParam, const MkString& Body, const MkMapString& MapHeader, const MkHttpResponseCb& ResponseCb, Uint32 TimeMilSeconds = 5000);
    CMkEventPool* GetEventPool() { return m_pEventPool; }
    CMkTimer* GetTimer() { return m_pTimer; }
protected:
private:
    CMkHttpClient*  GetFreeClient();
private:
    CMkTimer                                *m_pTimer;
    CMkEventPool                            *m_pEventPool;
    CMkLock                                 m_ClientLock;
    MkVector<CMkHttpClient*>                m_VecHttpClient;
};

#endif